class ApplicationController < ActionController::Base
  
  helper :all # include all helpers, all the time
  before_filter :maintain_session_and_user
  protect_from_forgery # :secret => '3ef815416f775098fe977004015c6193'
  
  def ensure_login
    unless @user
      #route_to_login 'You must be logged for this.'
      flash[:notice] = "Veuillez vous connecter svp !"
      redirect_to(new_session_path)
      session[:original_uri] = request.request_uri if request.get?
      #session[:return_to]=request.request_uri
    end
      
  end
  
  #def ensure_logout
  # if @user
  #   flash[:notice] = "You must logout before you can login"
  #   redirect_to(root_url)
  # end
  #end
  
  private
  
  def maintain_session_and_user
    if session[:id]
      if @application_session = Session.find_by_id(session[:id])
        @application_session.update_attributes(
          :ip_address => request.remote_addr,
          :path => request.path_info
        )
        @user = @application_session.account
      else
        session[:id] = nil
        redirect_to(root_url)
      end
    end
  end
    
  
end
